Assignment 1: Building a Better Contact Sheet

In the lectures for this week you were shown how to make a contact sheet for digital photographers, and how you can take one image and create nine different variants based on the brightness of that image. In this assignment you are going to change the colors of the image, creating variations based on a single photo. There are many complex ways to change a photograph using variations, such as changing a black and white image to either "cool" variants, which have light purple and blues in them, or "warm" variants, which have touches of yellow and may look sepia toned. In this assignment, you'll be just changing the image one color channel at a time

Your assignment is to learn how to take the stub code provided in the lecture (cleaned up below), and generate the following output image:

From the image you can see there are two parameters which are being varied for each sub-image. First, the rows are changed by color channel, where the top is the red channel, the middle is the green channel, and the bottom is the blue channel. Wait, why don't the colors look more red, green, and blue, in that order? Because the change you to be making is the ratio, or intensity, or that channel, in relationship to the other channels. We're going to use three different intensities, 0.1 (reduce the channel a lot), 0.5 (reduce the channel in half), and 0.9 (reduce the channel only a little bit).

For instance, a pixel represented as (200, 100, 50) is a sort of burnt orange color. So the top row of changes would create three alternative pixels, varying the first channel (red). one at (20, 100, 50), one at (100, 100, 50), and one at (180, 100, 50). The next row would vary the second channel (blue), and would create pixels of color values (200, 10, 50), (200, 50, 50) and (200, 90, 50).

Note: A font is included for your usage if you would like! It's located in the file readonly/fanwood-webfont.ttf

Need some hints? Use them sparingly, see how much you can get done on your own first! The sample code given in the class has been cleaned up below, you might want to start from that.

In [37]:
import PIL
from PIL import Image
from PIL import ImageEnhance
from PIL import ImageDraw
from PIL import ImageFont
from IPython.display import display

# read image and convert to RGB
image=Image.open("msi_recruitment.png")
image=image.convert('RGB')

# build a list of 9 images which have different brightnesses
enhancer=ImageEnhance.Brightness(image)
images=[]
for i in range(1, 10):
    images.append(enhancer.enhance(i/10))

# create a contact sheet from different brightnesses
first_image=images[0]
contact_sheet=PIL.Image.new(first_image.mode, (first_image.width*3,first_image.height*3))
x=0
y=0

for img in images:
    # Lets paste the current image into the contact sheet
    contact_sheet.paste(img, (x, y) )
    # Now we update our X position. If it is going to be the width of the image, then we set it to 0
    # and update Y as well to point to the next "line" of the contact sheet.
    if x+first_image.width == contact_sheet.width:
        x=0
        y=y+first_image.height
    else:
        x=x+first_image.width

# resize and display the contact sheet
contact_sheet = contact_sheet.resize((int(contact_sheet.width/2),int(contact_sheet.height/2) ))
#display(contact_sheet)
In [38]:
# Split the given image into channels R,G,B 

r,g,b = image.split()
singlechannel = [r,g,b]
In [39]:
# 9 images to be attached on canvas in format : 
#
#   (0.1R)GB    (0.5R)GB   (0.9R)GB   
#   R(0.1)GB    R(0.5)GB    R(0.9)GB
#   RG(0.1)B    RG(0.5)B    RG(0.9)B 
#
#________________________________________________

# attach channel information in text box below each image in every row 
In [40]:
# Create font object with provided font, set font type and font size 
font = ImageFont.truetype(
    "readonly/fanwood-webfont.ttf", 75)
In [41]:
# 3 lists containing single channel images of varying intensity 0.1 , 0.5 , 0.9 

Rimages = []
Gimages = []
Bimages = []

for i in [0.1,0.5,0.9]:
    new = r.point(lambda a: a * i)
    Rimages.append(new)
for i in [0.1,0.5,0.9]:
    new = g.point(lambda a: a * i)
    Gimages.append(new)
for i in [0.1,0.5,0.9]:
    new = b.point(lambda a: a * i)
    Bimages.append(new)
    
In [42]:
# creating rows for canvas 
firstrow = []
secondrow = []
thirdrow = []

# Creating single images to put in canvas rowwise, and adding text to be added at thier bottom. 

intensity = [0.1,0.5,0.9]

for R , i in zip(Rimages,intensity): # create a tuple with first image and its intensity as an integer
    new = Image.merge('RGB', (R, g, b)) # creating new image by merging with decreased R channel and B,G original channel
    Rtext = PIL.Image.new('RGB', (R.width,R.height + 100)) # added space to add text 
    Rtext.paste(new) # added newly created image with space for text 
    drawing_object = ImageDraw.Draw(Rtext) # drawing object 
    drawing_object.text((5,R.height + 20) , text = f"channel 0 intensity {i}" , font=font  ) # adding bottom text under each image
    firstrow.append(Rtext) # adding al images of first row 
    
for G, i in zip(Gimages, intensity):
    new = Image.merge('RGB', (r, G, b))
    Gtext = PIL.Image.new('RGB', (G.width, G.height + 100))
    Gtext.paste(new)
    drawing_object = ImageDraw.Draw(Gtext)
    drawing_object.text((5, G.height + 20),
                        text=f"channel 1 intensity {i}", font=font)
    secondrow.append(Gtext)
for B, i in zip(Bimages, intensity):
    new = Image.merge('RGB', (r, g, B))
    Btext = PIL.Image.new('RGB', (B.width, B.height + 100))
    Btext.paste(new)
    drawing_object = ImageDraw.Draw(Btext)
    drawing_object.text((5, B.height + 20),
                        text=f"channel 2 intensity {i}", font=font)
    secondrow.append(Btext)

allimages = firstrow[:] + secondrow[:] + thirdrow[:] # all images to be added on canvas 
In [43]:
# Create a canvas 3*Width of original Image and 3*(heignt + height of textbox= )

final_canvas = PIL.Image.new('RGB', (allimages[0].width*3,(allimages[0].height)*3))
In [44]:
# putting images on canvas with textbox below them 
x=0
y=0

for img in allimages:
    # pasting img to canvas
    
    final_canvas.paste(img, (x, y) )
    # Now we update our X position. If it is going to be the width of the image, then we set it to 0
    # and update Y as well to point to the next "line" of the contact sheet.
    if x + image.width == final_canvas.width:
        x = 0
        y = y + image.height + 100 
    else:
        x = x + image.width
In [46]:
display(final_canvas)
In [ ]: